package org.dawnmew.jpurewave;

/**
 * Keeps track of two {@link MusicInstance} objects to enable smooth playback
 * and point-to-point looping.
 *
 * @author dawnmew
 * @since 1.0
 *
 * Licensed under the MIT License.
 *
 * Copyright (c) 2012 dawnmew
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
import java.util.logging.Level;
import java.util.logging.Logger;

public class MusicCore extends Thread
{

    private String archiveFile;
    private MusicInstance startCore = null;
    private MusicInstance secondCore = null;
    public final Object readyLock = new Object();
    private boolean ready = false;
    private final Object pauseLock = new Object();
    private boolean paused = true;
    private final Object stopLock = new Object();
    private boolean stopping = false;
    private SoundSystem parent = null;
    private boolean nowCore = false;

    MusicCore(String pUrl, SoundSystem parent)
    {
        archiveFile = pUrl;
        this.parent = parent;
    }

    @Override
    public void run()
    {
        while (true)
        {
            startCore = new MusicInstance(archiveFile + "A.ogg", this);
            startCore.start();
            startCore.waitUntilReady();
            secondCore = new MusicInstance(archiveFile + "B.ogg", this);
            secondCore.start();
            synchronized (readyLock)
            {
                ready = true;
                readyLock.notifyAll();
            }
            nowCore = false;
            synchronized (pauseLock)
            {
                while (paused)
                {
                    try
                    {
                        pauseLock.wait();
                    } catch (InterruptedException ex)
                    {
                        Logger.getLogger(MusicCore.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
            startCore.play();
            startCore.waitUntilDone();
            nowCore = true;
            while (true)
            {
                synchronized (stopLock)
                {
                    if (stopping)
                    {
                        stopping = false;
                        break;
                    }
                }
                secondCore.play();
                secondCore.waitUntilDone();
            }
        }
    }

    public void waitUntilReady()
    {
        synchronized (readyLock)
        {
            while (!ready)
            {
                try
                {
                    readyLock.wait();
                } catch (InterruptedException ex)
                {
                    Logger.getLogger(MusicCore.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public void pause()
    {
        waitUntilReady();
        synchronized (pauseLock)
        {
            paused = true;
            pauseLock.notifyAll();
        }
        (nowCore ? secondCore : startCore).pause();
    }

    public void play()
    {
        waitUntilReady();
        synchronized (pauseLock)
        {
            paused = false;
            pauseLock.notifyAll();
        }
        (nowCore ? secondCore : startCore).play();
    }

    public void reset()
    {
        pause();
        synchronized (readyLock)
        {
            ready = false;
            readyLock.notifyAll();
        }
        synchronized (stopLock)
        {
            startCore.end();
            secondCore.end();
            stopping = true;
        }
        (nowCore ? secondCore : startCore).play();
    }

    public SoundSystem getParent()
    {
        return this.parent;
    }

    public String getSource()
    {
        return archiveFile;
    }
}